package Square;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import Monopol.AssetGroups.AllTransportations;
import Monopol.AssetGroups.AssetGroup;
import Monopol.MonopolExceptions.InsufficientFunds;
import Player.Player;


/**
 * @author Avia Tam
 * @author Assaf Miron
 *
 */
public class Transportation extends Square implements Negotiable {
	private AllTransportations transportationGroup; // The Transportation Group - Holds all the Transportations 
	private Double [] stay_price; /* 
		Price of use (or Staying in this Place), This Array has two Values:
		0: Not all the Transportation Services are owned by one Player
		1: All the Transportation Services are Owned by One Player
	*/ 
	private Player owner; // The Player who Owns this Square
	private Double cost; // The Cost of this Square
	private sameOwner same_owner_asset; /* This will indicate if all the Transportation Services 
	 								are Owned by One Player or not (0 or 1) */
	 
	//owner can be changed after a user losses,leaves the game or sale assets
	 //this parameter will be checked/set by the monopoly after each acquisition

	/**
	 * @param name
	 * @param stay_price
	 * @param cost
	 */
	public Transportation(String name, Double[] stay_price, Double cost, AllTransportations allTransportations) {
		super(name); // Construct the Square
		this.stay_price = stay_price; //Initialize the Transportation Stay Price Array
		this.cost = cost;//Initialize cost
		this.same_owner_asset = sameOwner.NOT_SAME_OWNER; // Initialize the Owner Asset to 0 - No Player has all the
		transportationGroup = allTransportations; // Initialize the Transportation Group
	}
	
	/**
	 * printout square description
	 */
	@Override
	public String toString() 
	{
		String message = super.toString() + " (Transportation)\n";
		
		// Check that there is a Owner
		if(owner != null)
			message = message +
			"Stay Price: " + stay_price[same_owner_asset.getValue()] + "\n" + 
			"Transportation Owner: " + owner.getName();
		else // No Owner
			message = message + 
			"Transportation Cost: " + cost.toString() + "\n" + 
			"Stay Price: \n" +
			"\tNot all Transportations owned by one Player: " + stay_price[0] + "\n" +
			"\tAll Transportations are owned by one Player: " + stay_price[1] + "\n" +
			"Transportation Owner: NONE";

		// Return the Message
		return message;
	}
	
	/**
	  * Sets the Same Owner Assets Parameter to 1
	  * Indicates that all the Services are Owned by one Player 
	  */
	 public void setSame_owner_asset() {
	  // Will Accrue if all assets are owned by one player
	  this.same_owner_asset = sameOwner.ALL_ASSETS_SAME_OWNER;
	 }
	 
	 /**
	 * Sets the Same Owner Assets Parameter to 0
	 * Indicates that Not all the Services are owned by one Player 
	 */
	public void unsetSame_owner_asset() {
	  // Will Accrue if owner left game, lost or sold the asset
	  this.same_owner_asset = sameOwner.NOT_SAME_OWNER;
	 }
	
	/**
	 * @return The value of the Same owner assets parameter 
	 */
	public Integer getSame_owner_asset() {
		return same_owner_asset.getValue();
	}
	
	/**
	 * @return the Stay Price of this Transportation
	 */
	public Double getStay_price() {
		return stay_price[same_owner_asset.getValue()];
	}


	/**
	 * @param transportationGroup the transportationGroup to set
	 */
	public void setTransportationGroup(AllTransportations transportationGroup) {
		this.transportationGroup = transportationGroup;
	}


	/**
	 * @return the transportationGroup
	 */
	public AllTransportations getTransportationGroup() {
		return transportationGroup;
	}
	
	@Override
	public void setOwner(Player player)//receives square owner from game 
	{
		this.owner = player;
	}
	
	@Override
	public Player getOwner() 
	{
		return owner;
	}
	
	@Override
	public boolean isBuyable() {
		// Check if This Service has an Owner
		if(this.getOwner() == null)
			return true; // No Owner - Can be Bought
		else
			return false; // This Service has an Owner - Can't be bought 
	}

	/* (non-Javadoc)
	 * @see Square.Negotiable#isSellable()
	 */
	@Override
	public boolean isSellable() {
		// Check if this Service has an Owner or not
		if(this.getOwner() != null)
			return true; // The Owner can Sell this Service
		else
			return false; // There is no Owner to the Service
	}

	/* (non-Javadoc)
	 * @see Square.Negotiable#buy(Player.Player)
	 */
	@Override
	public void buy(Player player) throws InsufficientFunds {
		// Check if the Player have enough Funds to buy this Service
		if(player.getFunds() >= this.cost)
		{
			// Take the Money from the Player
			player.addFunds(-this.cost);
			// Set the Player as the Owner of this Service
			this.setOwner(player);
		}
		else
			// Insufficient Funds
			throw new InsufficientFunds("Insufficient Funds, can't buy ");
		
	}

	@Override
	public void sell(Player player) 
	{
		// Change the Owner of this Transportation
		this.owner = player;
	}
	
	@Override
	public boolean isAsset(){
		return true;
	}
	
	@Override
	public void deleteOwner() {
		// Clear Owner
		owner = null;
	}

	@Override
	public AssetGroup getGroup()
	{
		return transportationGroup;
	}

	@Override
	public Double getCost()
	{
		return this.cost;
	}
	
	@Override
	public Color getColor()
	{
		// Check if there is an Owner for this Transportation
		if (owner != null)
			// Return the Owner Player Color
			return owner.getColor();
		else
			// Return a Default Color
			return transportationGroup.getColor();
	}
	
	/**
	 * @return The Transportation Details
	 */
	public String getDetails()
	{
		String retDetails;
		
		retDetails = "<HTML>Transportation Cost: " + cost.toString() + "<BR>" + 
		"Stay Price: <BR>" +
		"\tNot all Transportations owned by one Player: " + "<B>" + stay_price[0] + "</B><BR>" +
		"\tAll Transportations are owned by one Player: " + "<B>" + stay_price[1] + "</B><BR>" +
		"Transportation Owner: ";

		// Check that there is a Owner
		if(owner != null)
		{			
			retDetails = retDetails + "<B>" + owner.getName()+ "</B>";
		}
		else // No Owner
		{
			retDetails = retDetails + "<B>NONE</B>";
		}
		
		// Return the City Details
		return retDetails + "</HTML>";
	}
	
	@Override
	public JPanel draw()
	{
		// Create the Main Panel
		JPanel pnDetails = new JPanel();
		pnDetails.setLayout(new FlowLayout());
		pnDetails.setOpaque(false);
		
		// Create the Detail Labels
		JLabel lblDetails = new JLabel(getDetails(),JLabel.CENTER);
		
		
		// Add the Details Label to the Center
		pnDetails.add(lblDetails);

		// Return the Details Panel
		return pnDetails;
	}

}
